package com.hhkj.admin.question.common.mybatis.handler;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONWriter;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.hhkj.admin.question.model.extend.Option;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.util.List;

/**
 * OptionListHandler
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/5/16 16:53
 */
@Slf4j
@MappedJdbcTypes(JdbcType.VARCHAR)
public class OptionListHandler extends AbstractJsonTypeHandler<List<Option>>
{
    @Override
    protected List<Option> parse(String json)
    {
        return JSONArray.parseArray(json, Option.class);
    }

    @Override
    protected String toJson(List<Option> obj)
    {
        return JSONArray.toJSONString(obj,
                JSONWriter.Feature.WriteNullListAsEmpty,
                JSONWriter.Feature.WriteMapNullValue,
                JSONWriter.Feature.WriteNullStringAsEmpty
        );
    }
}
